package cn.iocoder.yudao.module.student.service.collectJob;

import cn.iocoder.yudao.module.student.dal.dataobject.collectCompany.CollectCompanyDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.student.controller.app.collectJob.vo.*;
import cn.iocoder.yudao.module.student.dal.dataobject.collectJob.CollectJobDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.student.convert.collectJob.CollectJobConvert;
import cn.iocoder.yudao.module.student.dal.mysql.collectJob.CollectJobMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.student.enums.ErrorCodeConstants.*;

/**
 * 收藏职位 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CollectJobServiceImpl implements CollectJobService {

    @Resource
    private CollectJobMapper collectJobMapper;

    @Override
    public Long createCollectJob(AppCollectJobCreateReqVO createReqVO) {
        // 插入
        CollectJobDO collectJob = CollectJobConvert.INSTANCE.convert(createReqVO);
        collectJobMapper.insert(collectJob);
        // 返回
        return collectJob.getId();
    }

    @Override
    public void updateCollectJob(AppCollectJobUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateCollectJobExists(updateReqVO.getId());
        // 更新
        CollectJobDO updateObj = CollectJobConvert.INSTANCE.convert(updateReqVO);
        collectJobMapper.updateById(updateObj);
    }

    @Override
    public void createOrUpdateCollectJob(@Valid AppCollectJobUpdateReqVO updateReqVO) {
        CollectJobDO collectJobDO=collectJobMapper.selectOne("job_id",updateReqVO.getJobId(),"student_id",updateReqVO.getStudentId());
        if(updateReqVO.getStatus().equals(0)){//取消
            if (collectJobDO != null) {
                collectJobMapper.deleteById(collectJobDO);
            }
        }
        if(updateReqVO.getStatus().equals(1)){//收藏
            if (collectJobDO == null) {
                CollectJobDO insetCollectJobDO=new CollectJobDO();
                insetCollectJobDO.setJobId(updateReqVO.getJobId());
                insetCollectJobDO.setCity(updateReqVO.getCity());
                insetCollectJobDO.setStudentId(updateReqVO.getStudentId());
                insetCollectJobDO.setId(null);
                insetCollectJobDO.setCollectTime(new Date());
                collectJobMapper.insert(insetCollectJobDO);
            }else{
                collectJobMapper.reduction(collectJobDO.getId());
            }
        }
    }

    @Override
    public void deleteCollectJob(Long id) {
        // 校验存在
        this.validateCollectJobExists(id);
        // 删除
        collectJobMapper.deleteById(id);
    }

    private void validateCollectJobExists(Long id) {
        if (collectJobMapper.selectById(id) == null) {
            throw exception(COLLECT_JOB_NOT_EXISTS);
        }
    }

    @Override
    public CollectJobDO getCollectJob(Long id) {
        return collectJobMapper.selectById(id);
    }

    @Override
    public List<CollectJobDO> getCollectJobList(Collection<Long> ids) {
        return collectJobMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CollectJobDO> getCollectJobPage(AppCollectJobPageReqVO pageReqVO) {
        return collectJobMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CollectJobDO> getCollectJobList(AppCollectJobExportReqVO exportReqVO) {
        return collectJobMapper.selectList(exportReqVO);
    }

    @Override
    public Boolean isCollect(AppCollectJobPageReqVO pageReqVO){
        CollectJobDO collectJobDO=collectJobMapper.selectOne("job_id",pageReqVO.getJobId(),"student_id",pageReqVO.getStudentId());
        if(collectJobDO!=null){
            return true;
        }
        return false;
    }
}
